package com.nutransit;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Browser;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mylibrary.mylibrary.parsing.AsyncTaskInterface;
import com.mylibrary.mylibrary.parsing.AsyncTaskUtility;
import com.mylibrary.mylibrary.util.CommonCode;
import com.nutransit.commoncode.Webservice_parametrs;

public class Registration_Activity extends Activity implements Webservice_parametrs
{

	private EditText ed_First_Name, ed_ph_no, ed_password, ed_Email_Id;
	private Button bt_done;
	private CheckBox cb_Terms_and_Conditions;
    private ImageView iv_Share_Facebook,iv_Share_Twitter,iv_Share_Youtube,iv_share_instagram;
	
	private String str_first_name, str_ph_no, str_password, str_email_id, str_type;
	private HashMap<String, String> hashMap;

	private Context context;
	private Resources resources;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_registration);

		str_type = getIntent().getStringExtra("type").toString();
		System.out.println("Type of value :" + str_type);

		init_var();
	}

	private void init_var()
	{

		context = Registration_Activity.this;
		resources = context.getResources();
		
		ed_First_Name = (EditText) findViewById(R.id.ed_First_Name);
		ed_ph_no = (EditText) findViewById(R.id.ed_ph_no);
		ed_password = (EditText) findViewById(R.id.ed_password);
		ed_Email_Id = (EditText) findViewById(R.id.ed_Email_Id);
		
		cb_Terms_and_Conditions = (CheckBox) findViewById(R.id.cb_Terms_and_Conditions);
		setLinks(cb_Terms_and_Conditions, Registration_Activity.this);
		
		iv_Share_Facebook = (ImageView) findViewById(R.id.iv_Share_Facebook);
		iv_Share_Twitter = (ImageView) findViewById(R.id.iv_Share_Twitter);
		iv_Share_Youtube = (ImageView) findViewById(R.id.iv_Share_Youtube);
		iv_share_instagram = (ImageView) findViewById(R.id.iv_share_instagram);

		bt_done = (Button) findViewById(R.id.bt_done);
		bt_done.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				check_validation();
			}

		});
		iv_Share_Facebook.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				CommonCode.Open_Link_In_Browser("https://www.facebook.com/pages/NuTransit/793351750674882",context);
			}
		});
		iv_Share_Twitter.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				CommonCode.Open_Link_In_Browser("https://twitter.com/nutransit",context);
			}
		});
		iv_Share_Youtube.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				CommonCode.Open_Link_In_Browser("https://www.youtube.com/channel/UChU7nb7HhkCKC1SkYJxpiDw",context);
			}
		});
		iv_share_instagram.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				CommonCode.Open_Link_In_Browser("http://instagram.com/nutransit",context);
			}
		});
	}

	private void check_validation()
	{
		str_first_name = ed_First_Name.getText().toString().trim();
		str_password = ed_password.getText().toString().trim();
		str_email_id = ed_Email_Id.getText().toString().trim();
		str_ph_no = ed_ph_no.getText().toString().trim();

		if (str_first_name.length()==0)
		{
			CommonCode.showMessage(resources.getString(R.string.Please_enter_the_valid_first_name), context);
		}
		else if (str_password.length() < 6)
		{
			CommonCode.showMessage(resources.getString(R.string.Password_must_be_minimum_of_6_digits), context);
		}
		else if (!CommonCode.checkEmailValidOrNot(str_email_id))
		{
			CommonCode.showMessage(resources.getString(R.string.Please_enter_the_valid_email_id), context);
		}
		else if (str_ph_no.length() < 10)
		{
			CommonCode.showMessage(resources.getString(R.string.Please_enter_the_valid_phone_number), context);
		}
		else if(!cb_Terms_and_Conditions.isChecked())
			CommonCode.showMessage(resources.getString(R.string.You_need_to_accept_Terms_and_Conditions_before_sign_up), context);
		else
		{
			String strURL = URL_SIGNUP_USER;
			hashMap = new HashMap<String, String>();
			hashMap.put(TAG_username, str_first_name);
			hashMap.put(TAG_password, str_password);
			hashMap.put(TAG_email, str_email_id);
			hashMap.put(TAG_phno, str_ph_no);
			if (str_type.equalsIgnoreCase(TAG_cab))
			{
				hashMap.put(TAG_dtype, "c");
				strURL = URL_SIGNUP_DRIVER;
			}
			else if (str_type.equalsIgnoreCase(TAG_van))
			{
				strURL = URL_SIGNUP_DRIVER;
				hashMap.put(TAG_dtype, "v");
			}
			new AsyncTaskUtility(context, hashMap, strURL).getJSONResponse(new AsyncTaskInterface()
			{
				@Override
				public void getJSONObjectFromAsynkTask(JSONObject json)
				{
					try
					{
						if (json != null)
						{
							if(json.has(TAG_message))
								CommonCode.showMessage(json.getString(TAG_message), context);
							if (json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
							{
								finish();
							}
						}
					}
					catch(JSONException e)
					{
						e.printStackTrace();
					}
				}
			});
		}
	}
	public void setLinks(TextView tv, Activity activity)
	{
		String text = tv.getText().toString().trim();
		String[] linkPatterns = { "(Terms and Conditions)"};
		for (String str : linkPatterns)
		{
			Pattern pattern = Pattern.compile(str);
			Matcher matcher = pattern.matcher(tv.getText());
			while (matcher.find())
			{
				int x = matcher.start();
				int y = matcher.end();
				final android.text.SpannableString f = new android.text.SpannableString(tv.getText());
				InternalURLSpan span = new InternalURLSpan(activity);
				span.text = text.substring(x, y);
				f.setSpan(span, x, y, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				tv.setText(f);
			}
		}
		tv.setLinkTextColor(Color.RED);
		tv.setLinksClickable(true);
		tv.setMovementMethod(LinkMovementMethod.getInstance());
		tv.setFocusable(false);
	}

	class InternalURLSpan extends android.text.style.ClickableSpan
	{
		public String text;
		Activity activity;

		public InternalURLSpan(Activity activity)
		{
			this.activity = activity;
		}

		@Override
		public void onClick(View widget)
		{
			handleLinkClicked(text, activity);
		}
	}

	public void handleLinkClicked(String value, Activity activity)
	{
		if (value.startsWith("http"))
		{
			// handle http links
			Uri uri = Uri.parse(value);
			final Intent i = new Intent(android.content.Intent.ACTION_VIEW, uri);
			i.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
			activity.startActivityForResult(i, 1);
		}
		else if (value.startsWith("@"))
		{
			// handle @links
		}
		else if (value.startsWith("#"))
		{
			// handle #links
		}
		else if (value.startsWith("Terms and Conditions"))
		{
			// handle #links
			startActivity(new Intent(context, Activity_WebView.class));
		}
	}
}
